package com.baiku.sanhos.controller;

import com.baiku.sanhos.mapper.RegMapper;
import com.baiku.sanhos.mapper.UserMapper;
import com.baiku.sanhos.mapper.UserPerMapper;
import com.baiku.sanhos.pojo.entity.User;
import com.baiku.sanhos.pojo.vo.RegVO;
import com.baiku.sanhos.response.ResultVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pa/users")
@Api(tags = "注册管理")
public class RegController {

    @Autowired
     UserPerMapper userPerMapper;


    @Autowired
     private UserMapper userMapper;

    @Autowired
    private RegMapper regMapper;
    private static final String REDIS_LONGIN_PRE = "REG:";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;



    @PostMapping("/reg")
    public ResultVO reg(@RequestBody RegVO regVO){
        //查看是否存在该手机号
        List<User> userList = userMapper.loadByPhone(regVO.getPhone());


        if(userList.size()!=0){

                  return new ResultVO(4008,"注册失败，该手机号已绑定过账号！");
            }
        String redisCode =stringRedisTemplate.opsForValue().get(REDIS_LONGIN_PRE+regVO.getPhone());

        String redisCode1=redisCode.trim().replace("\"",""); //去双引号
            //验证码判断

        if(redisCode1.equals(regVO.getCode())){

                    Map<String, Object> userParams = new HashMap<>();
                    userParams.put("username", regVO.getUsername());
                    /*加密之后再储存*/
                    BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
                    String password=passwordEncoder.encode(regVO.getPassword());
                    userParams.put("password", password);
                    userParams.put("gender", regVO.getGender());

                    userParams.put("phone", regVO.getPhone());
                    userParams.put("flag", "2");

                    userParams.put("nickname", regVO.getNickname());
                    userParams.put("status","0");
                    LocalDateTime currentDateTime = LocalDateTime.now();
                    // 将当前时间格式化为 MySQL 中的 DATETIME 字符串
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    String gmtCreate = currentDateTime.format(formatter);
                    userParams.put("gmtCreate",gmtCreate);
                    regMapper.insertAll(userParams);

                    userParams.put("birthday", regVO.getBirthday());
                    userParams.put("job", regVO.getJob());
                    userParams.put("address",regVO.getAddress());
                    userMapper.insertAll(userParams);


                     return new ResultVO(2008,"注册成功");


                }
             return new ResultVO(4008,"验证码错误");




    }}
